package com.yhny.lykj.yuhannongye.Adapter;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.longsichao.lscframe.net.HttpConnection;
import com.longsichao.lscframe.view.LSCImageView;
import com.longsichao.lscframe.view.LSCToast;
import com.yhny.lykj.yuhannongye.R;
import com.yhny.lykj.yuhannongye.app.BaseRefreshListAdapter;
import com.yhny.lykj.yuhannongye.model.BaseModel;
import com.yhny.lykj.yuhannongye.model.OrderListModel;
import com.yhny.lykj.yuhannongye.net.NetHelper;
import com.yhny.lykj.yuhannongye.tools.Tools;
import com.yhny.lykj.yuhannongye.ui.OrderInfoActivity;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.Response;

/**
 * OrderListAdapter
 * 订单列表
 */
public class OrderPayListAdapter extends BaseRefreshListAdapter<OrderPayListAdapter.ViewHolder> {
    private List<OrderListModel.ListEntity> list;
    private Context context;
    private BaseRefreshListAdapter.OnItemClickListener listener = null;

    public OrderPayListAdapter(List<OrderListModel.ListEntity> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    protected void onBindBaseViewHolder(ViewHolder holder, int position) {
        OrderListModel.ListEntity entity = list.get(position);
        Resources resources = holder.imageView1.getContext().getResources();
        int height = resources.getDimensionPixelOffset(R.dimen.item_order_small_width);
        int width = resources.getDimensionPixelOffset(R.dimen.item_order_small_width);
        String imageUrl1 = "";

        if (!TextUtils.isEmpty(entity.pic)) {
            imageUrl1 = entity.pic;
        }

        if (!TextUtils.isEmpty(imageUrl1)) {
            holder.imageView1.setPlaceHolder(R.drawable.normal_pic).setTargetSize(width, height).setImageURI(Uri.parse(imageUrl1));
        }else{
            holder.imageView1.setVisibility(View.GONE);
        }

        holder.typeTv.setText("订单编号："+entity.ordernumber);
        holder.titleView.setText(entity.person);
        holder.numView.setText("价格： ￥ "+entity.pay);
        final int pos = position;
        final String orderId = entity.orderid;


    }
    /**
     * 删除订单
     * @param orderId
     */
    private void delOrder(String orderId, final int pos){
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        Request request = new Request.Builder().url(NetHelper.Order.delOrder(orderId).getUrl()).get().build();
        HttpConnection.getHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        LSCToast.show(context, "服务器请求失败!");
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String string = response.body().string();
                new Handler(Looper.getMainLooper()).post(new Runnable() {

                    @Override
                    public void run() {
                        if (!Tools.isGoodJson(string)){
                            LSCToast.show(context, "服务器请求失败!");
                        }else {
                            Gson gson = new GsonBuilder().serializeNulls().create();
                            BaseModel userInfo = gson.fromJson(string, BaseModel.class);
                            if (userInfo == null || userInfo.equals("")) {
                                LSCToast.show(context, "服务器请求失败!");
                            }else if (userInfo.status==NET_ERROR||userInfo.status!=NET_SUSSEC){
                                LSCToast.show(context, userInfo.msg);

                            } else {
                                LSCToast.show(context, userInfo.msg);
                                list.remove(pos);
                                notifyDataSetChanged();
                            }
                        }
                    }
                });
            }
        });
    }
    /**
     * 确认收货
     * @param orderId
     */
    private void doneOrder(String orderId, final int pos){
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        Request request = new Request.Builder().url(NetHelper.Order.doneOrder(orderId).getUrl()).get().build();
        HttpConnection.getHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {

                    @Override
                    public void run() {
                        LSCToast.show(context, "服务器请求失败!");
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String string = response.body().string();
                new Handler(Looper.getMainLooper()).post(new Runnable() {

                    @Override
                    public void run() {
                        if (!Tools.isGoodJson(string)){
                            LSCToast.show(context, "操作失败!");
                        }else {
                            Gson gson = new GsonBuilder().serializeNulls().create();
                            BaseModel userInfo = gson.fromJson(string, BaseModel.class);
                            if (userInfo == null || userInfo.equals("")) {
                                LSCToast.show(context, "操作失败!");
                            }else if (userInfo.status==NET_ERROR||userInfo.status!=NET_SUSSEC){
                                LSCToast.show(context, userInfo.msg);
                            } else {
                                LSCToast.show(context, userInfo.msg);
                                list.get(pos).orderstate=ORDER_DONE;
                                notifyDataSetChanged();
                            }
                        }
                    }
                });
            }
        });
    }
    @Override
    protected int getLayoutResId(int viewType) {

        return R.layout.item_order_list;
    }

    @Override
    protected ViewHolder onCreateBaseViewHolder(View itemView) {
        return new ViewHolder(itemView, listener);
    }

    public static class ViewHolder extends BaseRefreshListAdapter.ViewHolder {

        private TextView titleView;
        private TextView typeTv;
        private TextView numView;
        private LSCImageView imageView1;
        public ViewHolder(View itemView, OnItemClickListener listener) {
            super(itemView, listener);
            imageView1 = (LSCImageView) itemView.findViewById(R.id.item_img1);
            numView = (TextView) itemView.findViewById(R.id.item_num);
            titleView = (TextView) itemView.findViewById(R.id.item_title);
            typeTv = (TextView) itemView.findViewById(R.id.item_shop);
        }

    }
}
